package com.ce.pms.house.web;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.ce.pms.base.web.PmsBaseAction;
import com.ce.pms.common.util.UpLoadFile;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.house.service.IBuildingService;
import com.ce.pms.house.service.IProjectService;
import com.ce.pms.house.vo.BuildingVO;
import com.ce.pms.house.vo.ImageVO;
import com.sitechasia.webx.core.annotation.Read;

public class BuildingAction extends PmsBaseAction{

	public String initBuildingMain(@Read(key="buildingId")Long buildingId,
			@Read(key="buildingName")String buildingName,
			@Read(key="areaId")Long areaId,
			@Read(key="projectId")Long projectId){
		this.getRequest().setAttribute("buildingId", buildingId);
		this.getRequest().setAttribute("buildingName", buildingName);
		this.getRequest().setAttribute("areaId", areaId);
		this.getRequest().setAttribute("projectId", projectId);
		return "buildingMain";
	}

	public void generationMaxSeqNoByProjectId(@Read(key="projectId")Long projectId) {

		try {
		this.renderText(buildingService.generationMaxSeqNoByProjectId(projectId).toString());
		} catch(Exception ex){
			this.renderText("false");
		}

	}

	public void validationFloor(@Read(key="keyword")Long keyword,
								@Read(key="homeFrom")Integer homeFrom,
								@Read(key="homeTo")Integer homeTo){

	this.renderText(buildingService.validationFloor(keyword, homeFrom, homeTo));


	}

	public String initBuildingAdd(@Read(key="areaId")Long areaId ,
			@Read(key="hasMoreProject")String hasMoreProject ){

		BuildingVO buildingVO = new BuildingVO();

		buildingVO.setAreaId(areaId);

		this.getRequest().setAttribute("hasMoreProject", hasMoreProject);
		this.getRequest().setAttribute("buildingVO", buildingVO);

		//获取期数列表
		this.getRequest().setAttribute("projectList", projectService.getProjectVoListByAreaId(areaId));
		//获取耐火等级列表
		this.getRequest().setAttribute("fireproofingLevelList", buildingService.getFireproofingLevelList());
		//获取人防等级列表
		this.getRequest().setAttribute("personSafeguardList", buildingService.getPersonSafeguardList());
		//获取抗震等级列表
		this.getRequest().setAttribute("quakeproofLevelList", buildingService.getQuakeproofLevelList());


		return "buildingAdd";
	}

	public String initBuildingEdit(@Read(key="buildingId")Long buildingId,
			                      @Read(key="areaId")Long areaId,
			                      @Read(key="hasMoreProject")String hasMoreProject){

		this.getRequest().setAttribute("hasMoreProject", hasMoreProject);

       //获取期数列表
		this.getRequest().setAttribute("projectList", projectService.getProjectVoListByAreaId(areaId));
		//获取耐火等级列表
		this.getRequest().setAttribute("fireproofingLevelList", buildingService.getFireproofingLevelList());
		//获取人防等级列表
		this.getRequest().setAttribute("personSafeguardList", buildingService.getPersonSafeguardList());
		//获取抗震等级列表
		this.getRequest().setAttribute("quakeproofLevelList", buildingService.getQuakeproofLevelList());

		this.getRequest().setAttribute("buildingVO", buildingService.getBuildingVOById(buildingId));

		return "buildingEdit";
	}

	/**
	 * 功能描述:查询楼栋的详细
	 * @param
	 * @return
	 *
	 * Create author:xuhaidui
	 * Create on:2008-7-17
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public String initBuildingDetail(@Read(key="buildingId")Long buildingId){

		this.getRequest().setAttribute("buildingVO", buildingService.getBuildingVOById(buildingId));

		return "buildingDetail";
	}

	public String addBuilding()throws Throwable{
		UpLoadFile upLoadFile=new UpLoadFile();
		BuildingVO buildingVO = new BuildingVO();
		List <ImageVO> list=upLoadFile.parseFormAndUpload(this.getRequest(), buildingVO);
		buildingVO.setCreator(this.getUserId());
		ImageVO layoutImageVo= new ImageVO();
		Set<ImageVO> ichnographys = new HashSet<ImageVO>();


		for(ImageVO vo:list) {
			//规划图
			if(vo.getFieldName().indexOf("layoutImageVo")>-1){
				layoutImageVo.setName(vo.getName());
				layoutImageVo.setPath(vo.getPath());
			}
			//平面图集合
			if(vo.getFieldName().indexOf("ichnographys")>-1){
				ImageVO ichnographysVO = new ImageVO();
				ichnographysVO.setName(vo.getName());
				ichnographysVO.setPath(vo.getPath());
				ichnographys.add(ichnographysVO);

			}
		}

		buildingVO.setLayoutImageVo(layoutImageVo);
		buildingVO.setIchnographys(ichnographys);



		BuildingVO outVO=buildingService.addBuilding(buildingVO);

		this.getRequest().setAttribute("buildingId", outVO.getKeyword());
		this.getRequest().setAttribute("flag", "buildingOprator");
		return "onlyRefreshHouse";
	}

	public String editBuilding()throws Throwable{

		UpLoadFile upLoadFile=new UpLoadFile();
		BuildingVO buildingVO = new BuildingVO();
		List <ImageVO> list=upLoadFile.parseFormAndUpload(this.getRequest(), buildingVO);
		buildingVO.setCreator(this.getUserId());
		ImageVO layoutImageVo= new ImageVO();
		Set<ImageVO> ichnographys = new HashSet<ImageVO>();


		for(ImageVO vo:list) {
			//规划图
			if(vo.getFieldName().indexOf("layoutImageVo")>-1){
				layoutImageVo.setName(vo.getName());
				layoutImageVo.setPath(vo.getPath());
			}
			//平面图集合
			if(vo.getFieldName().indexOf("ichnographys")>-1){
				ImageVO ichnographysVO = new ImageVO();
				ichnographysVO.setName(vo.getName());
				ichnographysVO.setPath(vo.getPath());
				ichnographys.add(ichnographysVO);

			}
		}
		upLoadFile.wrapImageSet("ichnographys", ichnographys);
		upLoadFile.wrapImage("layoutImageVo", layoutImageVo);
		buildingVO.setLayoutImageVo(layoutImageVo);
		buildingVO.setIchnographys(ichnographys);

		buildingService.editBuilding(buildingVO);

		this.getRequest().setAttribute("flag", "buildingOprator");
		return "onlyRefreshHouse";
	}

	/**
	 * 功能描述:对楼栋名称进行验证（增加楼栋时）
	 * @param
	 * @return
	 *
	 * Create author:xuhaidui
	 * Create on:2008-7-18
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
    public void checkBuildingNameIsExist(@Read(key="areaId")Long areaId,
    		@Read(key="name")String buildingName) {
    	try {

    		if(buildingService.checkBuildingNameIsExist(areaId,buildingName)){
    			this.renderText(this.getLocaleMessage("house:building_name_exist_exception"));

    		} else {
    			this.renderText("true");
    		}

        }catch(Exception e){
        	this.renderText("false");
    	}
    }

	/**
	 * 功能描述:对楼栋名称进行验证（修改楼栋时）
	 * @param
	 * @return
	 *
	 * Create author:xuhaidui
	 * Create on:2008-7-18
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
    public void checkNameIsExist(@Read(key="areaId")Long areaId,
    		@Read(key="keyword")Long buildingId,
    		@Read(key="name")String buildingName) {
    	try {

    		if(buildingService.checkBuildingNameIsExist(areaId,buildingId,buildingName)){
    			this.renderText(this.getLocaleMessage("house:building_name_exist_exception"));

    		} else {
    			this.renderText("true");
    		}

        }catch(Exception e){
        	this.renderText("false");
    	}
    }

    /**
	 * 功能描述:验证楼栋编码是否存在（楼栋修改时）
	 * @param
	 * @return
	 *
	 * Create author:xuhaidui
	 * Create on:2008-7-18
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
    public void editCheckSeqNoIsExist(@Read(key="projectId")Long projectId,
    		                         @Read(key="keyword")Long buildingId,
    		                         @Read(key="seqNo")Integer seqNo) {
    	try {

    		if(buildingService.checkSeqNoIsExist(projectId,buildingId,seqNo)){
    			this.renderText(this.getLocaleMessage("house:building_seqno_exist_exception"));

    		} else {
    			this.renderText("true");
    		}

        }catch(Exception e){
        	this.renderText("false");
    	}
    }

    /**
	 * 功能描述:验证楼栋编码是否存在（楼栋增加时）
	 * @param
	 * @return
	 *
	 * Create author:xuhaidui
	 * Create on:2008-7-18
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
    public void addCheckSeqNoIsExist(@Read(key="projectId")Long projectId,
            @Read(key="seqNo")Integer seqNo) {
		try {

			if(buildingService.checkSeqNoIsExist(projectId,seqNo)){
				this.renderText(this.getLocaleMessage("house:building_seqno_exist_exception"));
			} else {
				this.renderText("true");
			}

		}catch(Exception e){
			this.renderText(this.getLocaleMessage("house:building_no_choice_project_exception"));
		}
	}

	public String deleteBuilding(@Read(key="buildingId")Long buildingId){
        try {
            List<ImageVO> imageList = new ArrayList<ImageVO>();

            this.buildingService.deleteBuilding(buildingId, imageList);

            if (imageList.size() > 0) {
                for (ImageVO imageVO : imageList) {
                    UpLoadFile.deleteFile(imageVO);
                }
            }
        }catch(BusinessException be){
            throw be;
        } catch (Exception e){
            super.logger.error(e.getMessage(), e);
            throw new BusinessException("house:building_delete_exception",BusinessException.FAILURE);
        }

        this.getRequest().setAttribute("flag", "buildingOprator");

		return "onlyRefreshHouse";
	}

	private IBuildingService buildingService;
	private IProjectService projectService;


	public void setBuildingService(IBuildingService buildingService) {
		this.buildingService = buildingService;
	}

	public void setProjectService(IProjectService projectService) {
		this.projectService = projectService;
	}


}
